// HSS.Forms.AutoCompleteTextBox.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       AutoCompleteTextBox.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Runtime.InteropServices;
	using System.Windows.Forms;
	#endregion

	#region AutoCompleteTextBox
	/// <summary>
	/// The AutoCompleteTextBox provides for the autocompletion as a user types text
	/// </summary>
	public class AutoCompleteTextBox : System.Windows.Forms.TextBox
	{
		#region SHLWAPI SHAutoComplete API

		[DllImport("SHLWAPI", CharSet = CharSet.Auto, SetLastError = true)]
		static extern int SHAutoComplete(IntPtr hwndEdit,
			SHAutoCompleteFlags dwFlags);

		[Flags]
		enum SHAutoCompleteFlags : uint
		{
			/// <summary>
			/// Currently (FileSystem | UrlAll)
			/// </summary>
			Default = 0x00000000,

			/// <summary>
			/// This includes the File System as well as the rest 
			/// of the shell (Desktop\My Computer\Control Panel\)
			/// </summary>
			FileSystem = 0x00000001,

			/// <summary>
			/// URLs in the User's History and URLs in the User's 
			/// Recently Used list.
			/// </summary>
			UrlAll = (UrlHistory | UrlMRU),

			/// <summary>
			/// URLs in the User's History
			/// </summary>
			UrlHistory = 0x00000002,

			/// <summary>
			/// URLs in the User's Recently Used list.
			/// </summary>
			UrlMRU = 0x00000004,

			/// <summary>
			///  Use the tab to move thru the autocomplete 
			///  possibilities instead of to the next dialog/window
			///  control.
			/// </summary>
			UseTab = 0x00000008,

			/// <summary>
			/// Don't AutoComplete non-File System items.
			/// </summary>
			FileSysOnly = 0x00000010,

			/// <summary>
			/// Ignore the registry default and force the feature on.
			/// </summary>
			AutoSuggestForceOn = 0x10000000,

			/// <summary>
			/// Ignore the registry default and force the feature off.
			/// </summary>
			AutoSuggestForceOff = 0x20000000,

			/// <summary>
			/// Ignore the registry default and force the feature on. 
			/// (Also known as AutoComplete)
			/// </summary>
			AutoAppendForceOn = 0x40000000,

			/// <summary>
			/// Ignore the registry default and force the feature off. 
			/// (Also known as AutoComplete)
			/// </summary>
			AutoAppendForceOff = 0x80000000
		}

		#endregion

		private System.ComponentModel.Container components = null;

		/// <summary>
		/// AutoCompleteTextBox Constructor
		/// </summary>
		public AutoCompleteTextBox()
		{
			InitializeComponent();
		}

		bool _autocomplete_urls = false;
		bool _autocomplete_files = false;

		/// <summary>
		/// Indicates if we should AutoComplete URL's
		/// </summary>
		[Browsable(true),
		DesignOnly(true),
		Description("Gets or sets whether or not to autocomplete URLs."),
		Category("Behavior")]
		public bool AutoCompleteUrls
		{
			get { return this._autocomplete_urls; }
			set { this._autocomplete_urls = value; }
		}
		/// <summary>
		/// Indicates if we should AutoComplete Files
		/// </summary>
		[Browsable(true),
		DesignOnly(true),
		Description("Gets or sets whether or not to autocomplete file paths."),
		Category("Behavior")]
		public bool AutoCompleteFiles
		{
			get { return this._autocomplete_files; }
			set { this._autocomplete_files = value; }
		}
		/// <summary>
		/// The OnHandleCreated implementation
		/// </summary>
		/// <param name="e"></param>
		protected override void OnHandleCreated(EventArgs e)
		{
			if (this.AutoCompleteUrls || this.AutoCompleteFiles)
			{
				Application.OleRequired();

				SHAutoCompleteFlags flags = SHAutoCompleteFlags.AutoSuggestForceOn |
					SHAutoCompleteFlags.AutoAppendForceOn;

				if (this.AutoCompleteUrls) flags |= SHAutoCompleteFlags.UrlAll;
				if (this.AutoCompleteFiles) flags |= SHAutoCompleteFlags.FileSystem;

				SHAutoComplete(this.Handle, flags);
			}

			base.OnHandleCreated(e);
		}
		/// <summary>
		/// The Dispose method
		/// </summary>
		/// <param name="disposing"></param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion
	}
	#endregion
}